import 'package:flutter/material.dart';
import 'package:get/get.dart';

class CheckBoxFormField extends FormField<bool> {
  CheckBoxFormField({
    FormFieldSetter<bool>? onSaved,
    FormFieldValidator<bool>? validator,
    bool? initialValue = false,
    bool? autovalidate = false,
    required Widget title,
  }) : super(
          onSaved: onSaved,
          validator: validator,
          initialValue: initialValue ?? false,
          autovalidateMode: autovalidate! ? AutovalidateMode.always : AutovalidateMode.disabled,
          builder: (FormFieldState<bool> field) {
            return Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.start,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Checkbox(
                      value: field.value,
                      onChanged: field.didChange,
                    ),
                    title,
                  ],
                ),
                if (field.hasError)  //自定义错误信息
                  Text(
                    field.errorText!,
                    style: const TextStyle(color: Colors.red, fontSize: 14),
                  ).marginOnly(left: 15),
              ],
            );
          },
        );
}
